/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean.session;

import entity.CompletedGoodsEntity;
import entity.PriceListEntity;
import entity.ProductEntity;
import entity.PurchaseOrderEntity;
import java.util.ArrayList;
import javax.ejb.Stateless;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author wei
 */
@Stateless
public class PurchaseOrderEntityFacade extends AbstractFacade<PurchaseOrderEntity> {
    @PersistenceContext(unitName = "MacTeam-warPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public PurchaseOrderEntityFacade() {
        super(PurchaseOrderEntity.class);
    }
    
    public DataModel searchPurchaseOrderById(String attribute, String input) {
        Query query = em.createQuery("SELECT p FROM PurchaseOrder p" + " WHERE p." + attribute + " like " + "'%" + input + "%' AND p.archived != 'true'");
        return new ListDataModel(query.getResultList());
    }
    
    public DataModel searchPurchaseOrderByName(String attribute, String input) {
        Query queryCustomer = em.createQuery("SELECT s FROM PurchaseOrder s INNER JOIN s.customer c WHERE CONCAT(c.first" + attribute + ", ' ', c.last" + attribute + 
                ") LIKE '%" + input + "%' AND s.archived != 'true'");
        
        ArrayList query = new ArrayList<Object>();

        for (Object o : queryCustomer.getResultList()) {
            query.add(o);
        }
        return new ListDataModel(query);
    }
    
    public CompletedGoodsEntity getPurchaseOrderProduct(String productName) {
        Query query = em.createQuery("Select p FROM CompletedGoods p");
        for(Object o : query.getResultList()) {
            CompletedGoodsEntity product = (CompletedGoodsEntity) o;
            if(product.getProductName().equals(productName))
                return product;
        }
        return null;
    }
    
       
   public DataModel getAllPurchaseOrderForCustomer(long id) {
        Query query = em.createQuery("SELECT s FROM PurchaseOrder s INNER JOIN s.customer c WHERE c.id = " + id + " AND s.archived != 'true'");
        ArrayList purchaseOrder = new ArrayList<PurchaseOrderEntity>();
        
        for (Object o : query.getResultList()) {
            PurchaseOrderEntity s = (PurchaseOrderEntity) o;
            purchaseOrder.add(s);
        }
        return new ListDataModel(purchaseOrder);
    }
   
   public ArrayList<PriceListEntity> getPriceList() {
        Query query = em.createQuery("SELECT p FROM PriceList p WHERE p.archived != 'true' ORDER BY p.tieredQuantity asc");
        ArrayList priceList = new ArrayList<PriceListEntity>();
        
        for (Object o : query.getResultList()) {
            PriceListEntity p = (PriceListEntity) o;
            priceList.add(p);
        }
        return priceList;
    }
   
   public DataModel getAllPurchaseOrder() {
        Query query = em.createQuery("SELECT p FROM PurchaseOrder p WHERE p.archived!= 'true'");
        return new ListDataModel(query.getResultList());
    }
    
}
